<template>
  <div class="running-time">
    <Navbar :bg="navBg" :title="navTitle" />
    <div class="items">
      <div class="item" v-for="item in r_runningItemsTop" :key="item.id">
        <div class="left">
          <div class="title">{{ item.title }}</div>
          <div class="description">已运行{{ item.time }}小时</div>
        </div>
        <div class="right" @click="onRest(item)">复位</div>
      </div>
    </div>
    <div class="items">
      <div class="item" v-for="item in r_runningItemsBottom" :key="item.id">
        <div class="left">
          <div class="title">{{ item.title }}</div>
          <div class="description">已运行{{ item.time }}小时</div>
        </div>
        <div class="right" @click="onRest(item)">复位</div>
      </div>
    </div>
  </div>
  <van-dialog v-model:show="runningShow" :show-confirm-button="false">
    <RunningDialog @emitConfirm="emitConfirm" />
  </van-dialog>
</template>

<script setup lang="ts">
import Navbar from '@/components/Navbar.vue'
import RunningDialog from '@/components/SettingsComp/RunningDialog.vue'
import { ref, watch } from 'vue'
import { runningItemsTop, runningItemsBottom } from '@/utils/enum.js'
import { sendToNative_sendUartData } from '@/utils/base.js'
import { verifySum } from '@/utils/utils.ts'

import { useStore } from 'vuex'
const store = useStore()

const navBg = ref('#fff')
const navTitle = ref('运行时间')
const runningShow = ref(false)
const currentIdx = ref<number>(0)
const r_runningItemsTop = ref(runningItemsTop)
const r_runningItemsBottom = ref(runningItemsBottom)

// 初始化数据
const init = () => {
  console.log('init==')
  const info = store.state.deviceInfo
  console.log(info, 'info')
  r_runningItemsTop.value[0].time = info.pdpTime
  r_runningItemsTop.value[1].time = info.dischargeAirTime
  r_runningItemsTop.value[2].time = info.blowAirTime

  r_runningItemsBottom.value[0].time = info.lowEffectTime
  r_runningItemsBottom.value[1].time = info.hightEffectTime
}
init()
watch(
  () => store.state,
  val => {
    console.log('store数据变化了==', val)
    const { deviceInfo } = val
    if (deviceInfo) {
      init()
    }
  },
  { deep: true }
)

const onRest = (item: any) => {
  console.log(item.id)
  currentIdx.value = item.id
  runningShow.value = true
}
const emitConfirm = (type: string) => {
  if (type === 'confirm') {
    console.log('confirm')
    const sum = verifySum(`020500${currentIdx.value}020000`)
    const hexVal = `F1F1020500${currentIdx.value}020000${sum}7E`
    sendToNative_sendUartData(hexVal)
    // 设置为 0
    if (currentIdx.value === 14) {
      r_runningItemsTop.value[0].time = 0
    } else if (currentIdx.value === 15) {
      r_runningItemsTop.value[1].time = 0
    } else if (currentIdx.value === 16) {
      r_runningItemsTop.value[2].time = 0
    } else if (currentIdx.value === 17) {
      r_runningItemsBottom.value[0].time = 0
    } else if (currentIdx.value === 18) {
      r_runningItemsBottom.value[1].time = 0
    }
  }
  runningShow.value = false
}
</script>
<style lang="less" scoped>
@import url(../assets/style/control.less);
</style>
